/*jslint browser: true, devel: true, bitwise: true, white: true, plusplus: true */
/*global $:true, define:true */

/**
 * @module Core
 */

define(function() {
"use strict";

/** 
 * @class EventDispatcher
 * @constructor
 */
var EventDispatcher = function() {};

EventDispatcher.prototype = {
	constructor: EventDispatcher,
	
	addListener: function(eventType, listener) {
		if (this.Listeners === undefined) {
			this.Listeners = {};
		}
		var listeners = this.Listeners;

		if (listeners[eventType] === undefined) {
			listeners[eventType] = [];
		}

		if (listeners[eventType].indexOf(listener) === -1) {
			listeners[eventType].push(listener);
		}
	},
	
	hasListener: function(eventType, listener) {
		if ( this.Listeners === undefined ) {
			return false;
		}
		
		var listeners = this.Listeners;
		if (listeners[eventType] !== undefined && listeners[eventType].indexOf(listener) !== -1) {
			return true;
		}
		
		return false;
	},
	
	removeListener: function(eventType,listener) {
		if ( this.Listeners === undefined ) {
			return;
		}
		
		var 
			listeners = this.Listeners,
			index = listeners[eventType].indexOf(listener);

		if (index !== -1) {
			listeners[eventType].splice(index,1);
		}
	},
	
	dispatchEvent: function(event) {
		if (this.Listeners === undefined) {
			return;
		}
		var 
			listeners = this.Listeners,
			listenerArray = listeners[event.type],
			i = 0, l;

		if (listenerArray !== undefined) {
			event.target = this;
			for (i,l = listenerArray.length; i < l; i++) {
				listenerArray[i].call(this,event);
				console.info('dispatched event:'+event.type);
			}

		}
	}
};

return EventDispatcher;
});
